package com.controller.v1;

import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.service.Impl.AdChannerlServiceImpl;
import org.apache.http.protocol.HTTP;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("channel")
public class AdChannelController {

    @Autowired
    private AdChannerlServiceImpl adChannerlService;

    //根据名字模糊分页查询频道数据
    @PostMapping
    public ResponseResult<AdChannel> findPageChannelByName(@RequestBody ChannelDto dto) {
        ResponseResult byNameAndPage = adChannerlService.findByNameAndPage(dto);
        if (byNameAndPage != null) {
            byNameAndPage.setCode(AppHttpCodeEnum.SUCCESS.getCode());
            return byNameAndPage;
        }

        ResponseResult<AdChannel> result = new ResponseResult<>();
        result.setCode(AppHttpCodeEnum.DATA_NOT_EXIST.getCode());
        return byNameAndPage;

    }
    //添加频道
    @PostMapping("/save")
    public ResponseResult saveChannel(@RequestBody AdChannel channel) {

        ResponseResult responseResult = adChannerlService.saveChannel(channel);

        return responseResult;
    }
    //根据id删除频道
    @DeleteMapping
    public ResponseResult deleteChannel(@RequestBody AdChannel channel){
        Integer channelId = channel.getId();
        ResponseResult result = adChannerlService.deleteChannel(channelId);
        if (result!=null){
            return result;
        }
        ResponseResult resultError = new ResponseResult();
        result.setCode(AppHttpCodeEnum.SERVER_ERROR.getCode());
        result.setErrorMessage("服务器内部错误，Service层返回值不存在");
        return resultError;
    }

    //修改频道信息
    @PutMapping
    public ResponseResult update(@RequestBody AdChannel channel){
        ResponseResult result = new ResponseResult();
        if (channel==null){
            result.setCode(AppHttpCodeEnum.PARAM_INVALID.getCode());
            result.setErrorMessage("参数不存在");
            return result;
        }
        result = adChannerlService.updateChannel(channel);
        if (result!=null){
            return result;
        }
        result.setCode(AppHttpCodeEnum.SERVER_ERROR.getCode());
        result.setErrorMessage("业务结果返回失败");
        return result;

    }
}
